//
//  PGViisitor.swift
//  PGWeibo
//
//  Created by Apple on 15/11/3.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class PGVisitor: UIView {
    /// 登录注册的代理属性

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
        
        // 设置背景颜色 - 灰度图 R = G = B，在 UI 元素中，大多数都使用灰度图，或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置访客界面的信息
    func setupVisitorInfo(imageName: String?, description: String?) {
        //判断是否为首页
        guard let iconName = imageName else {
            //为首页添加动画
            setAnimation()
            return;
        }
        coverView.hidden = true
        houseView.hidden = true
        iconView.image = UIImage(named: iconName)
        messageLabel.text = description
    }
    /// 点击注册
    @objc private func clickRegister() {
        
    }
    /// 点击登录
    @objc private func clickLogin() {
        
    }
    
    //图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜")
    //注册
    lazy var regisetButton: UIButton = UIButton(BackImage: "common_button_white_disable", title: "注册", textColor: UIColor.orangeColor())
    //登录
    lazy var loginButton: UIButton = UIButton(BackImage: "common_button_white_disable", title: "登录")
    //蒙版
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 添加动画
    private func setAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.byValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
    }
}
// MARK: - 设置UI界面
extension PGVisitor {
    private func setupView() {
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(regisetButton)
        addSubview(loginButton)
        
        
        // translatesAutoresizingMaskIntoConstraints 默认是 true，支持使用 setFrame 的方式设置控件位置
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //自动布局
        //图片
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //文字框
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册
        addConstraint(NSLayoutConstraint(item: regisetButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: regisetButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: regisetButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: regisetButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //登录
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //蒙版
        /**
        VFL : 可视化格式语言
        
        H 水平方向
        V 垂直方向
        | 边界
        [] 包装控件
        views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
        metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
        */
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":coverView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(buttonHeiht)-[login]", options: [], metrics: ["buttonHeiht":-35], views: ["mask":coverView, "login":loginButton]))
 
    }
}
